package com.mls.fivedistrict.server.impl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mls.fivedistrict.dao.VoteMapper;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Vote;
import com.mls.fivedistrict.pojo.VoteOption;
import com.mls.fivedistrict.server.VoteOptionService;
import com.mls.fivedistrict.server.VoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 投票 服务实现类
 * </p>
 *
 * @author Five Team
 * @since 2018-05-15
 */
@Service
public class VoteServiceImpl extends ServiceImpl<VoteMapper, Vote> implements VoteService {
    @Autowired
    private VoteMapper voteMapper;
    @Autowired
    private VoteOptionService voteOptionService;

    @Transactional
    @Override
    public boolean insertAndVoteOptions(Vote vote) {
        int row = voteMapper.insert(vote);
        if (row < 0) {
            return false;
        }
        if (vote.getVoteOptionList() == null || vote.getVoteOptionList().size() == 0) {
            return true;
        }
        for (VoteOption voteOption : vote.getVoteOptionList()) {
            voteOption.setVoteId(vote.getId());
        }
        boolean insert = voteOptionService.insertBatch(vote.getVoteOptionList());
        if (!insert) {
            throw new CustomerException("插入失败");
        }
        return true;
    }

    @Transactional
    @Override
    public boolean updateVoteAndVoteOption(Vote vote) {
        int row = voteMapper.updateById(vote);
        if (row <= 0) {
            return false;
        }
//        boolean delete = voteOptionService.delete(Condition.create().eq("vote_id", vote.getId()));
//        if (!delete) {
//            throw new CustomerException("修改失败");
//        }

        VoteOption voteOptionTmp = new VoteOption();
        voteOptionTmp.setDelete(true);
//        voteOptionTmp.setIsDelete(false);
        boolean stop = voteOptionService.update(voteOptionTmp, Condition.create().eq("vote_id", vote.getId()).eq("is_delete", false).eq("is_enable", true));
        if (!stop) {
            int count = voteOptionService.selectCount(Condition.create().eq("vote_id", vote.getId()).eq("is_delete", false).eq("is_enable", true));
            if (count != 0) {
                throw new CustomerException("修改失败");
            }
        }
        for (VoteOption voteOption : vote.getVoteOptionList()) {
            voteOption.setVoteId(vote.getId());
        }
        if (vote.getVoteOptionList() == null || vote.getVoteOptionList().size() == 0) {
            return true;
        }
        boolean insert = voteOptionService.insertOrUpdateBatch(vote.getVoteOptionList());
        if (!insert) {
            throw new CustomerException("修改失败");
        }
        return true;
    }

    @Override
    public Vote selectVoteAndVoteOption(Long voteId) {
        Vote vote = voteMapper.selectVoteAndVoteOptions(voteId);
        return vote;
    }

    @Override
    public List<Vote> selectListAndVoteOption(Wrapper wrapper) {
        List<Vote> voteList = voteMapper.selectListAndVoteOption(wrapper);
        return voteList;
    }

    @Override
    public boolean addVoteCount(Long id, Long version, BigDecimal total, int voteCount) {
        long insert = voteMapper.addVoteCount(id, version, total, voteCount);
        if (insert <= 0) {
            return false;
        }
        return true;
    }
}
